package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentFeedbackBinding;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizReportAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.ToastUtils;

import retrofit2.Response;

public class FeedbackActivity extends AbsTitleBarActivity {
    private static final String EXTRA_ID = "object_id";
    private static final String EXTRA_TYPE = "extra_type";

    public static void start(Context context, int id, boolean isQuiz) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        intent.putExtra(EXTRA_ID, id);
        intent.putExtra(EXTRA_TYPE, isQuiz);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return getString(R.string.title_feedback);
    }
    @Override
    protected BaseFragment onCreateContentFragment() {
        return new FeedbackFragment();
    }
    @Override
    protected void setupTitleView() {
        setOperateTitle(getString(R.string.head_operate_submit));
    }
    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putInt(EXTRA_ID, getIntent().getIntExtra(EXTRA_ID, 0));
        args.putBoolean(EXTRA_TYPE, getIntent().getBooleanExtra(EXTRA_TYPE, true));
        return args;
    }
    @Override
    public void onHeadActionClick(View view) {
        super.onHeadActionClick(view);
        ((FeedbackFragment) mFragment).onSubmitAction();
    }
    public static class FeedbackFragment extends BaseFragment {
        private FragmentFeedbackBinding mBinding;
        private int mExtraId;
        private PbBean.PbSignBaseReportCategory mReportType;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_feedback, container, false);
            initViews();
            return mBinding.getRoot();
        }

        private void initViews() {
            mExtraId = getArguments().getInt(EXTRA_ID);
            boolean quiz = getArguments().getBoolean(EXTRA_TYPE);
            mReportType = quiz ? PbBean.PbSignBaseReportCategory.REPORTCATEGORY_QUESTION : PbBean.PbSignBaseReportCategory.REPORTCATEGORY_ANSWER;
        }
        public void onSubmitAction() {
            String title = getReportTitle();
            String content = getContent();
            if (StringUtils.isNullEmpty(title)) {
                ToastUtils.show(getActivity(), R.string.toast_feedback_title_isnull);
                return;
            }
            if (StringUtils.isNullEmpty(content)) {
                ToastUtils.show(getActivity(), R.string.toast_feedback_content_isnull);
                return;
            }
            showProgressDialog();
            QuizReportAction action = QuizReportAction.newInstance(getActivity(), mExtraId,title,content,mReportType);
            onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionAddReport>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResQuestionAddReport> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    closeProgressDialog();
                    if (HttpHelper.showError(getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doFeedbackSuccess();
                }
            }));
        }
        private String getContent() {
            return StringUtils.filterNullAndTrim(mBinding.etContent.getText().toString());
        }
        private String getReportTitle() {
            return StringUtils.filterNullAndTrim(mBinding.etTitle.getText().toString());
        }
        private void doFeedbackSuccess() {
            ToastUtils.show(getActivity(), R.string.toast_submit_success);
            getActivity().finish();
        }
    }


}
